package com.lzh.swagger.builder;

import com.google.common.base.Function;
import com.google.common.base.Optional;
import com.google.common.base.Predicate;
import org.springframework.util.AntPathMatcher;
import org.springframework.util.ClassUtils;
import springfox.documentation.RequestHandler;

/**
 * @author zhehen.lu
 * @date 2024/7/3 14:46
 */
public class LzhRequestHandlerSelectors {
    public LzhRequestHandlerSelectors() {
        // TODO document why this constructor is empty
    }

    public static Predicate<RequestHandler> basePackage(final String basePackage) {
        return input -> (Boolean)LzhRequestHandlerSelectors.declaringClass(input).transform(LzhRequestHandlerSelectors.handlerPackage(basePackage)).or(true);
    }

    private static Function<Class<?>, Boolean> handlerPackage(final String basePackage) {
        return input -> {
            AntPathMatcher antPathMatcher = new AntPathMatcher();
            return antPathMatcher.match(basePackage, ClassUtils.getPackageName(input));
        };
    }

    private static Optional<? extends Class<?>> declaringClass(RequestHandler input) {
        return Optional.fromNullable(input.declaringClass());
    }
}
